package org.smile.homework02.arthas.oneCase;

import lombok.Data;

@Data
public class Vehicle implements Generator {
    //1.0生成车
    public Vehicle() {
        this.number = generateNumber();
        this.type = generateType(); //生成随机类型
    }

    //1.1车牌号
    private String number;
    //1.2车的类型
    private String type;
    //1.3占用的停车位
//    private ParkingSpace parkingSpace;

    //2.进入停车场
    public void enterParkingLot(ParkingLot parkingLot, Integer time) {
        parkingLot.handleEnterParkingLot(this, time);
        System.out.println(this.number + "进入停车场");

        Integer parkingSpaceNumber = lookForParkingSpace(parkingLot);
        if (parkingSpaceNumber != null) {
            enterParkingSpace(parkingLot, parkingSpaceNumber);
        } else {
            leaveParkingLot(parkingLot, time);
        }

    }

    //3.找停车位
    public Integer lookForParkingSpace(ParkingLot parkingLot) {

        for (ParkingSpace parkingSpace : parkingLot.getParkingSpaces()) {
            if (!parkingSpace.isOccupied() && parkingSpace.getType().equals(this.getType())) {
                System.out.println(this.getNumber() + "找停车位");
                return parkingSpace.getNumber();
            }
        }
        System.out.println(this.getNumber() + "没找停车位");
        return null;


    }

    //4.进入停车位
    public void enterParkingSpace(ParkingLot parkingLot, Integer parkingSpaceNumber) {
        parkingLot.handleEnterParkingSpace(this, parkingSpaceNumber);
        System.out.println(this.number + "进入停车位");
    }

    //5.离开停车位
    public void leaveParkingSpace(ParkingLot parkingLot, Integer parkingSpaceNumber) {
        parkingLot.handleLeaveParkingSpace(parkingSpaceNumber);
        System.out.println(this.number + "离开停车位");
    }

    //6.离开停车场
    public void leaveParkingLot(ParkingLot parkingLot, Integer time) {
        parkingLot.handleLeaveParkingLot(this, time);
        System.out.println(this.number + "离开停车场");
    }

    //7.决定是否离开: false 不离开，true 离开
    public boolean decideLeave() {
        int random = generateDecimal();
        switch (random) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                return false;
            case 8:
            case 9:
            default:
                return true;
        }
    }

    public void describe() {
        System.out.format("车牌号：%s, [车位类型：%s]\n", this.getNumber(), this.getType());
    }
}
